<template>
  <div class="app-container">
    <el-card style="overflow: visible">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="课程名称" prop="courseName">
          <el-input
            v-model="queryParams.courseName"
            placeholder="请输入课程名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="课程类别" prop="courseType">
          <el-select
            v-model="queryParams.courseType"
            placeholder="请选择课程类别"
            clearable
          >
            <el-option
              v-for="dict in dict.type.ocss_course_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属专业" prop="professionId">
          <el-select
            v-model="queryParams.professionId"
            placeholder="请选择所属专业"
            clearable
          >
            <el-option
              v-for="item in professionOptions"
              :key="item.id"
              :label="item.professionName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="校区/系" prop="deptId">
          <treeselect
            v-model="queryParams.deptId"
            :options="deptOptions"
            :show-count="true"
            style="width: 200px"
            placeholder="请选择归属校区或系"
          />
        </el-form-item>
        <el-form-item label="学期" prop="semesterId">
          <el-select
            v-model="queryParams.semesterId"
            placeholder="请选择所属学期"
            clearable
          >
            <el-option
              v-for="item in semesterOptions"
              :key="item.id"
              :label="item.semesterName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top: 10px">
      <el-row :gutter="10" class="mb8">
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="courseList">
        <el-table-column label="课程名称" align="center" prop="courseName" />
        <el-table-column label="学分" width="50" align="center" prop="credit" />
        <el-table-column label="课程类别" align="center" prop="courseType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.ocss_course_type"
              :value="scope.row.courseType"
            />
          </template>
        </el-table-column>
        <el-table-column label="上课时段" align="center" prop="classTimer" />
        <el-table-column label="上课地点" align="center" prop="classLocation" />

        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="查看选课学生"
              placement="top"
            >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit-outline"
                @click="toDetail(scope.row)"
                v-if="scope.row.status != 0"
                >录入成绩</el-button
              >
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { teacherCourseList } from "@/api/system/achievement";

import { listTeacherAll } from "@/api/system/teacher";
import { listProfession } from "@/api/system/profession";
import { listSemester } from "@/api/system/semester";
import { deptTreeSelect } from "@/api/system/user";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Course",
  dicts: ["ocss_course_type", "ocss_course_status"],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      deptOptions: undefined, // 校区列表
      teacherOptions: undefined, // 教师列表
      professionOptions: undefined, // 专业列表
      semesterOptions: undefined, // 学期列表

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程信息表格数据
      courseList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
        courseType: null,
        professionId: null,
        deptId: null,
        semesterId: null,
        status: null,
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.getTeacherList();
    this.getSemesterList();
    this.getProfessionList();
  },
  methods: {
    /** 查询校区或系下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    getTeacherList() {
      listTeacherAll().then((res) => {
        this.teacherOptions = res.data;
      });
    },
    getProfessionList() {
      listProfession().then((res) => {
        this.professionOptions = res.data;
      });
    },
    getSemesterList() {
      listSemester().then((res) => {
        this.semesterOptions = res.data;
      });
    },
    /** 查询课程信息列表 */
    getList() {
      this.loading = true;
      teacherCourseList(this.queryParams).then((response) => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    toDetail(row) {
      this.$router.push({
        path: `/achievement/detail/${row.id}/${row.courseName}`,
      });
    },
  },
};
</script>
